﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using XText = Chimp.Core.ChimpText.ChimpTextCache;


namespace Chimp.Core
{
    public class ChimpText
    {
        private static Bitmap TmpMap;

        private static ChimpMaterial TextMat;
        private static ChimpEffect TextEffect;
        public static Font CurrentFont { get; set; }

        public static void Init()
        {
            SetFont(FontFamily.GenericMonospace, 10);
            TmpMap = new Bitmap(512, 512);
            TextMat = new ChimpMaterial("DefaultTextMaterial");
            TextEffect = new ChimpEffect("", "Content/Shader/XenoMaterial_Text.fragment");
            TextMat.Effect = TextEffect;
        }


        public static void SetFont(FontFamily family, int size)
        {
            CurrentFont = new Font(family, size);
        }

        public static ChimpTextCache CreateText(string text)
        {
            Graphics tg = Graphics.FromImage(TmpMap);
            SizeF siz = tg.MeasureString(text, CurrentFont);


            var bitmap = new Bitmap((int) siz.Width + 20, (int) siz.Height + 20);
            Graphics g = Graphics.FromImage(bitmap);


            g.SmoothingMode = SmoothingMode.HighQuality;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            g.Clear(Color.Transparent);

            g.DrawString(text, CurrentFont, Brushes.White, 0, 0);

            // assign the bitmap to a globally defined one
            var cache = new ChimpTextCache(bitmap, (int) siz.Width, (int) siz.Height);
            tg.Dispose();

            g.Dispose();

            return cache;
        }

        public static void DrawText(ChimpTextCache cache, int x, int y, Color color)
        {
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Disable(EnableCap.AlphaTest);


            GL.Color4(color);

            TextMat.DiffuseMap = cache.Visual;


            cache.Visual.Bind(0);

            TextEffect.Use();

            TextEffect.SetInt("MaterialDiffuseMap", 0);
            TextEffect.SetVec4("MaterialDiffuseColor",
                               new Vector4(color.R/255.0f, color.G/255.0f, color.B/255.0f, color.A/255.0f));
           

            GL.Begin(BeginMode.Quads);

            GL.TexCoord2(0, 0);
            GL.Vertex2(x, y);
            GL.TexCoord2(1, 0);
            GL.Vertex2(x + cache.Width + 20, y);
            GL.TexCoord2(1, 1);
            GL.Vertex2(x + cache.Width + 20, y + cache.Height + 25);
            GL.TexCoord2(0, 1);
            GL.Vertex2(x, y + cache.Height + 25);

            GL.End();

            TextEffect.Fin();

            cache.Visual.Unbind(0);
        }

        #region Nested type: ChimpTextCache

        public class ChimpTextCache
        {
            public ChimpTextCache(Bitmap bitmap, int width, int height)
            {
                Visual = new ChimpTexture2D(bitmap);
                Width = width;
                Height = height;
            }

            public ChimpTexture2D Visual { get; set; }
            public int Width { get; set; }
            public int Height { get; set; }
        }

        #endregion
    }
}